<!--
 * @description: 新增不是会员充值
 * @Author: latiao
 * @Date: 2021-11-09 09:00:12
 * @LastEditors: latiao
 * @LastEditTime: 2022-03-14 10:32:17
-->
<template>
<view class="page">
      <view class="title">充值金额</view>
      <view class="input">
        <input type="digit" style="text-align: center;" maxlength="11" :value="customPrice" @input="inputPrice"
          placeholder="请输入充值金额">
      </view>
      <view class="payNow">
        <view class="payNow__btn"  @tap="openMem">立即充值</view>
      </view>
    

    <u-popup :show="payTypeFlag" @close="onClose" >
      <view class="payConter">
        <view class="payConter__top">
          <van-icon name="arrow-left" style="margin-right:40rpx" @tap="onClose"></van-icon>
          确认付款
        </view>
        <view class="payConter__num">￥{{customPrice}}</view>
        <view class="payConter__openingMode ">
          <view>充值金额</view>
          <view>{{customPrice}}</view>
        </view>
        <view class="payConter__openingMode ">
          <view>赠送金额</view>
          <view>{{discountAmount || 0}}</view>
        </view>
        <view class="payConter__openingMode ">
          <view>到账金额</view>
          <view>{{getPrice}}</view>
        </view>
        <view class="payConter__openingMode ">
          <view>付款方式</view>
          <view>微信支付</view>
        </view>
        <view class="center">
          <view @tap="paySure" class="payConter__btn">确认付款</view>
        </view>
      </view>
    </u-popup>
    </view>
</template>

<script lang='ts'>
  import {
    Component,
    Vue
  } from 'vue-property-decorator'
  import {
    userPay,
  } from "@/api/modules/member";

  @Component({})
export default class newRecharge extends Vue {
  customPrice: number = 0
  discountAmount: number = 0 // 赠送金额
  payTypeFlag: boolean = false // 购买弹窗
  timer
  flag

 
throttle(func, wait = 300, immediate = true) {
	if (immediate) {
		if (!this.flag) {
			this.flag = true;
			// 如果是立即执行，则在wait毫秒内开始时执行
			typeof func === 'function' && func();
			this.timer = setTimeout(() => {
				this.flag = false;
			}, wait);
		}
	} else {
		if (!this.flag) {
			this.flag = true
			// 如果是非立即执行，则在wait毫秒内的结束处执行
			this.timer = setTimeout(() => {
				this.flag = false
				typeof func === 'function' && func();
			}, wait);
		}
		
	}
}

  paySure() {
    const that=this
     this.throttle(
      that.userPay,10000
     )
    }
  userPay(){
    userPay({
          paymentType: 1,
          discountAmount: this.discountAmount,
          payAmount: this.customPrice,
        })
        .then(res => {
          uni.requestPayment({
            timeStamp: res.wxResult.timeStamp,
            nonceStr: res.wxResult.nonceStr,
            package: res.wxResult.packageValue,
            signType: res.wxResult.signType,
            paySign: res.wxResult.paySign,
            success: () => {
              this.setData({
                payTypeFlag: false
              });
              uni.showToast({
                title: "充值成功",
                icon: "success"
              });
              setTimeout(() => {
                 uni.navigateTo({
                  url: `/subcontract/pages/balanceValue/balanceValue`,
                 }); 
              }, 1500);              
            },
            fail: e => {
              const isCancel = msg => msg.includes("cancel");
              if (!isCancel(e.errMsg)) {
                uni.showToast({
                  title: "支付失败"
                });
              }
            },
          });
        })
        .catch(err => {
          uni.showToast({
            title: err || "支付失败"
          });
        })
  }


    /**
     * @method
     * @description 输入价格
     */
    inputPrice(e) {
      this.setData({
        customPrice: e.detail.value,
        getPrice: e.detail.value,
      });
      // this.filterRule();
    }



    
    /**
     * 弹起确认支付
     */
    openMem() {
      if (!this.customPrice) return null;
      this.setData({
        payTypeFlag: true
      });
      return null;
    }

    /**
     * 收起弹窗
     */
    onClose() {
      this.setData({
        payTypeFlag: false
      });
    }

}
</script>

<style lang="scss" scoped>
 @mixin middle-center {
    display: flex;
    justify-content: center;
    align-items: center;
  }

  .page {
    padding-left: 30rpx;
    padding-right: 30rpx;
    height: 100vh;
    background: #FFF;
  }

  .title {
    font-size: 38rpx;
    padding-top: 40rpx;
    padding-bottom: 40rpx;
  }


  .rechage {
    display: flex;
    justify-content: space-between;
    width: 100%;
    flex-wrap: wrap;

    &__item {
      height: 140rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 20rpx;
      margin-bottom: 30rpx;
      padding-left: 30rpx;
      padding-right: 30rpx;
      border: 1rpx solid #d8dce5;

      &--content {
        display: flex;
        justify-content: center;
        flex-wrap: wrap;
        font-size: 38rpx;
      }
    }
  }

  .input {
    height: 100rpx;
    border-radius: 20rpx;
    margin-top: 40rpx;
    @include middle-center;
    background-color: #e5e5e5;
  }

  .payNow {
    display: flex;
    margin-top: 120rpx;
    justify-content: center;
  }

  .payNow__btn {
    width: 600rpx;
    height: 80rpx;
    background: linear-gradient(to right, #EBCC89, #B4893B);
    border-radius: 50rpx;
    color: white;
    font-size: 32rpx;
    @include middle-center;

  }

  .payNow__nbtn {
    background: #888;
  }

  .payConter {
    height: 650rpx;
    padding: 20rpx;
  }

  .payConter__top {
    font-size: 30rpx;
    display: flex;
    align-items: center;
  }

  .payConter__num {
    padding: 40rpx;
    font-size: 80rpx;
    @include middle-center;
  }

  .payConter__openingMode {
    margin-bottom: 20rpx;
    font-size: 32rpx;
    padding-left: 20rpx;
    padding-right: 20rpx;
    color: #666666;
    display: flex;
    justify-content: space-between;
  }

  .payConter__btn {
    margin: 50rpx auto 30rpx;
    width: 450rpx;
    height: 80rpx;
    border-radius: 50rpx;
    background: linear-gradient(to right, #EBCC89, #B4893B);
    color: white;
    font-size: 30rpx;
    // box-shadow: 0px 0px 40px #FEDBDF;
    @include middle-center;
  }
</style>